package com.mycompony.shopping.shangguan.sys.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mycompony.shopping.shangguan.sys.context.ApplicationContextUtil;
import com.mycompony.shopping.shangguan.sys.dao.UserMapper;
import com.mycompony.shopping.shangguan.sys.model.Password;
import com.mycompony.shopping.shangguan.sys.model.User;
import com.mycompony.shopping.shangguan.sys.model.UserExample;
import com.mycompony.shopping.shangguan.sys.service.PasswordService;
import com.mycompony.shopping.shangguan.sys.service.UserService;
import com.mycompony.shopping.shangguan.sys.util.Constant;

public class UserServiceImpl implements UserService {

	private UserMapper userMapper;

	private PasswordService passwordService;
	
	public List<User> queryAllUser() {
		UserExample userExample= new UserExample();
		userExample.createCriteria().andIdEqualTo(new Long(1));
		return userMapper.selectByExample(userExample);
	}

	public UserMapper getUserMapper() {
		return userMapper;
	}

	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}


	public PasswordService getPasswordService() {
		return passwordService;
	}

	public void setPasswordService(PasswordService passwordService) {
		this.passwordService = passwordService;
	}

	public User loginUser(User user,Password password) {
		if (user == null || StringUtils.isEmpty(user.getUserid())) {
			return null;
		}
		UserService test =(UserService) ApplicationContextUtil.getBean("userService");
		
		UserExample userExample= new UserExample();
		
		userExample.createCriteria().andUseridEqualTo(user.getUserid()).andActiveEqualTo(Constant.active_success);
		
		List<User>  list = userMapper.selectByExample(userExample);
		
		if(list == null || list.isEmpty()||list.size()>1){
			return null;
		}
		
		String userid = list.get(0).getUserid();
		
		if (userid == null || StringUtils.isEmpty(userid)) {
			return null;
		}
		
		password.setUserid(userid);
		
		Password tempPassword = passwordService.validatePassword(password);
		
		if (tempPassword == null) {
			return null;
		}
		
		return list.get(0);
	}


}
